﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using System.Data;
using V5_Model;

namespace V5_DAL
{
    public class DALProductAsk
    {
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_ProductAsk");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ModelProductAsk model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into K_ProductAsk(");
            strSql.Append("AskTitle,AskContent,MemberID,ProductID,Passed,AddDateTime,GetIP)");
            strSql.Append(" values (");
            strSql.Append("@AskTitle,@AskContent,@MemberID,@ProductID,@Passed,@AddDateTime,@GetIP)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@AskTitle", SqlDbType.NVarChar,200),
					new SqlParameter("@AskContent", SqlDbType.NVarChar,300),
					new SqlParameter("@MemberID", SqlDbType.Int,4),
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@Passed", SqlDbType.Int,4),
					new SqlParameter("@AddDateTime", SqlDbType.DateTime),
					new SqlParameter("@GetIP", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.AskTitle;
            parameters[1].Value = model.AskContent;
            parameters[2].Value = model.MemberID;
            parameters[3].Value = model.ProductID;
            parameters[4].Value = model.Passed;
            parameters[5].Value = model.AddDateTime;
            parameters[6].Value = model.GetIP;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelProductAsk model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_ProductAsk set ");
            strSql.Append("AskTitle=@AskTitle,");
            strSql.Append("AskContent=@AskContent,");
            strSql.Append("MemberID=@MemberID,");
            strSql.Append("ProductID=@ProductID,");
            strSql.Append("Passed=@Passed,");
            strSql.Append("AddDateTime=@AddDateTime,");
            strSql.Append("GetIP=@GetIP");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@AskTitle", SqlDbType.NVarChar,200),
					new SqlParameter("@AskContent", SqlDbType.NVarChar,300),
					new SqlParameter("@MemberID", SqlDbType.Int,4),
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@Passed", SqlDbType.Int,4),
					new SqlParameter("@AddDateTime", SqlDbType.DateTime),
					new SqlParameter("@GetIP", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.AskTitle;
            parameters[2].Value = model.AskContent;
            parameters[3].Value = model.MemberID;
            parameters[4].Value = model.ProductID;
            parameters[5].Value = model.Passed;
            parameters[6].Value = model.AddDateTime;
            parameters[7].Value = model.GetIP;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_ProductAsk ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelProductAsk GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,AskTitle,AskContent,MemberID,ProductID,Passed,AddDateTime,GetIP from K_ProductAsk ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            ModelProductAsk model = new ModelProductAsk();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.AskTitle = ds.Tables[0].Rows[0]["AskTitle"].ToString();
                model.AskContent = ds.Tables[0].Rows[0]["AskContent"].ToString();
                if (ds.Tables[0].Rows[0]["MemberID"].ToString() != "")
                {
                    model.MemberID = int.Parse(ds.Tables[0].Rows[0]["MemberID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProductID"].ToString() != "")
                {
                    model.ProductID = int.Parse(ds.Tables[0].Rows[0]["ProductID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Passed"].ToString() != "")
                {
                    model.Passed = int.Parse(ds.Tables[0].Rows[0]["Passed"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDateTime"].ToString() != "")
                {
                    model.AddDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddDateTime"].ToString());
                }
                model.GetIP = ds.Tables[0].Rows[0]["GetIP"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,AskTitle,AskContent,MemberID,ProductID,Passed,AddDateTime,GetIP ");
            strSql.Append(" FROM K_ProductAsk ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,AskTitle,AskContent,MemberID,ProductID,Passed,AddDateTime,GetIP ");
            strSql.Append(" FROM K_ProductAsk ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "K_ProductAsk";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 1;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        #endregion  成员方法
    }
}
